package com.example.social.item_view

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import coil.compose.AsyncImage
import coil.compose.rememberAsyncImagePainter
import coil.request.ImageRequest
import com.example.social.R
import com.example.social.api.ThreadList
import com.example.social.model.ThreadModel
import com.example.social.model.UserModel
import com.example.social.navigation.Routes

@Composable
fun ThreadListPage(
    threadsAndUser: List<ThreadList>,
    navHostController: NavHostController,
) {
    LazyColumn(
        modifier = Modifier.fillMaxSize()
    ) {
        // 遍历数据并根据不同条件渲染不同布局
        items(threadsAndUser.toList()) { page ->
            // 如果线程内容很长，显示包含图片的布局
            ThreadItemWithImage(
                thread = page.threads!!,
                users = page.users!!,
                navHostController = navHostController,
            )
        }
    }
}

// 渲染带图片的线程项
@Composable
fun ThreadItemWithImage(
    thread: ThreadModel,
    users: UserModel,
    navHostController: NavHostController,
) {
    var isExpanded by remember { mutableStateOf(false) }
    val maxLines = if (isExpanded) Int.MAX_VALUE else 2
    val context = LocalContext.current


    Column(
        modifier = Modifier
            .fillMaxWidth()
            .padding(16.dp)
            .clickable {
                val route = Routes.ThreadDetailScreen.routes.replace("{data}", thread.tid)
                //进入详情页
                navHostController.navigate(route)
            }
    ) {
        Row(modifier = Modifier.clickable {
            val route = Routes.OtherUsers.routes.replace("{data}", users.uid)
            navHostController.navigate(route)
        }){
            // 用户头像
            AsyncImage(
                model = ImageRequest.Builder(context)
                    .data(users.imageurl)
                    .crossfade(true)
                    .error(R.drawable.undraw_male_avatar)
                    .build(),
                contentDescription = "User Image",
                modifier = Modifier
                    .size(40.dp)
                    .clip(CircleShape),
                contentScale = ContentScale.Crop
            )

            // 用户名
            Text(
                text = users.nickname ?: "Anonymous",
                style = TextStyle(
                    fontSize = 18.sp,
                    fontWeight = FontWeight.Bold
                ),
                modifier = Modifier.padding(start = 12.dp, top = 8.dp)
            )

        }
        Text(
            text = thread.thread,
            style = TextStyle(
                fontSize = 16.sp,
                lineHeight = 20.sp
            ),
            maxLines = maxLines,
            overflow = TextOverflow.Ellipsis,
            modifier = Modifier.padding(start = 12.dp, top = 8.dp)
        )

        // 更多按钮
        if (thread.thread.length > 50 && !isExpanded) {
            Text(
                text = "更多",
                style = TextStyle(
                    fontSize = 14.sp,
                    color = Color.Blue
                ),
                modifier = Modifier
                    .clickable { isExpanded = true }
                    .padding(start = 12.dp, top = 4.dp)
            )
        }

        // 图片
        if (thread.image.isNotEmpty()) {
            Card(
                shape = RoundedCornerShape(8.dp),
                modifier = Modifier
                    .padding(top = 12.dp)
                    .fillMaxWidth()
                    .heightIn(max = 250.dp)
            ) {
                Image(
                    painter = rememberAsyncImagePainter(model = thread.image),
                    contentDescription = "Post Image",
                    contentScale = ContentScale.Crop,
                    modifier = Modifier.fillMaxSize()
                )
            }
        }

        // 时间戳
        Text(
            text = thread.timestamp,
            style = TextStyle(
                fontSize = 12.sp,
                color = Color.Gray
            ),
            modifier = Modifier.padding(start = 12.dp, top = 8.dp)
        )

        // Divider
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .padding(top = 5.dp)
                .height(1.dp)
                .background(Color.LightGray)
        )
    }
}
